var WEB_SERVICE_URL = sureURL("Lists.asmx?WSDL");
var WEB_SERVICE_NAME = "LISTS";
var WS;
var funcCB_List;
var menuWindow;
var oWS = {};
var LISTS = "Lists"; //Web Service name
var Lists; //Web Service Object
var MenuID;
initWS();
function copyList(){
	var tbl = _listTable();
	if( !tbl )return alert("No list to save.");
	var bColor = tbl.borderColor;
	tbl.borderColor = "Gray";
	frmSave.text.value = tbl.outerHTML.replace(/<input[^>]+>/ig,"").replace(/style="[^"]+"/i,"");
	tbl.borderColor = bColor;
	frmSave.contentType.value = "application/x-msexcel";
	frmSave.fileName.value = _menuPath().split(">")[1]+".xls";
	frmSave.submit();
	return;
}
function listLoadNext(applID){
	getNextFromList();
}
function refreshOnSendToMenu(){setTimeout("listRefresh()",500)}
function page_Load(){
	attachToSendToMenu(refreshOnSendToMenu);
	attachToListLoadNext(listLoadNext,0);
}
function showApplInfo(td){
	var applID = fieldByName(td,"ApplicantID");
	var menuParam = applID ? "" : "&menuID="+MenuID;
	var w =	top.open("ShowApplicant.aspx?ApplID="+applID+menuParam,"AFC_"+applID,features("status=yes"));
	var count = 10;
	_setWindowFocus.proc = setInterval(function(){_setWindowFocus()},1000);
	//Local
	function _setWindowFocus(){
		try{w.focus();count=0;
		}catch(e){return;}
		if( !count ){
			//status = "Got Focus! "+_setWindowFocus.proc;
			return clearInterval(_setWindowFocus.proc);
		}
	}
}
function showSubMenuItem(menuID,params,action,attributes,heading,menuPath,onArrive){
var pn,cm,to,un,pw,hd,ep;
	MenuID = menuID;
	//status = 'menuID['+menuID+'],'+'params:['+params+'],'+'action:['+action+'],'+'attributes:['+attributes+'],'+'heading:['+heading+'],'+'menuPath:['+menuPath+"]";
	switch( params.toLowerCase() ){
		case 'pull':
			var applID = prompt("Applicant ID",'');
			if( applID ){
				var ret = WS(CO("PutApplToNewArrivals"),applID);
				showSOAPError(ret);
			}
		case '':
		case 'list':
			_listMenuID(menuID);
			var filters = {};
			eval(onArrive);
			oWS.cb = function(result){onListReady(result,menuID,filters)};
			oWS.co = ws.createCallOptions(action.replace(/[(].+/,""),pn,true,to,un,pw,hd,ep,_getParams(action));
			if( getList() )_menuPath(menuPath).split(">")[1];
			break;
		case 'eval':
			var showParams = ["status=no","resizable=yes","dialogWidth=100"];
			eval(action);
			break;
		case 'popup':
			var showParams = ["status=no","resizable=yes","dialogWidth=100"];
			var url = eval(action);
			if( url )menuWindow = showModelessDialog(url,null,showParams.join(";"));
			break;
		case 'window':
			var url = attributes.toLowerCase() == "eval" ? eval(heading) : action+(heading||"");
			if( url )menuWindow = open(url,"MENU_"+menuID,features("status=yes"));
			break;
		case 'link':
			try{menuWindow.close()}catch(e){}
			top.location.replace(action);
			break;
		case 'interactive':
			eval(action);
			break;
	}
	//if( getList() )spnMenuPath.innerText = menuPath;
	///Locals
	
	function _getParams(action){//debugger;
		var abc = "abcdefghij";
		var p;
		var a = action.match(/[(](.+)[)]/);
		a = !a ? new Array("menuID") : a[1].split(",");
		for( var i=0,j=0,aa=new Array(); i < a.length; i++ ){
			var s = unescape(a[i]);
			if( !s.match(/^['"]/) )aa[s] = eval(s);
			else aa[abc.charAt(j++)] = s.substr(1,s.length-2);
		}
		return aa;
	}
	// ***** interactives *****
	function listWhere(){
		var url = "/include/ShowRS.htm?servicePath="+escape("/Manager/Menu/Lists.asmx?");
		showModelessDialog(url+"&serviceFunc="+escape("ListWhere(null)")+"&onFilter=ListWhere&title=Find Applicant",{opener:self},"resizable:1;status:0");
	}
	// +++++ interactives +++++
}
function getList(){
	if( WS ){
		WS(oWS.cb,oWS.co);
		return true;
	}
	setTimeout("getList()",100);
}
function onListReady(result,menuID,filters){
	var bid = onListReady.bid;
	if( showSOAPError(result) )return;
	var tbl = _listTable();
	//if( tbl )tbl.removeBehavior(bid);
  if( contents.children[0] )
		contents.removeChild(contents.children[0]).outerHTML = "";
  contents.appendChild(divListTempl.cloneNode(true)).style.display="";
	tbl = contents.all.tblTempl;
	tbl.menuID = menuID;
	var dlTime = new Date();
	loadDSO(dso,result.value);
  showDSO(dso,tbl);
  for( var filter in filters )
		for( var i = 0; i < search.length; i++ )
			if( search[i].field.match(new RegExp("^"+filter+"$","i")) ){
				search[i].value = filters[filter];
				filterDSO(search[i]);
			}
	downLoadTime = new Date()-dlTime;
}
function getNextFromList(pos){
	if( !_listTable() )return alert("Pull the list to take a next applicant from.");
	listRefresh(pos);// refresh list and click on row at selected position
	_listTable().onrowclick = _loadNext;
	// Locals
	function _loadNext(){//loads first avalible applicant starting with current
		_listTable().onrowclick = null;
		var row = event.srcRow;
		var ret = WS(CO("LockNextApplicant"),fieldByName(row,"ApplicantID"),_listMenuID());
		if( showSOAPError(ret) )return;
		if( ret.value )return row.cells[0].click();
		var pos = _listPos(row)+1;
		if( pos >= _listRecordCount() )return getNextFromList(0);
		_listTable().onrowclick = _loadNext;
		_listRow(pos).click();
	}
}
function listRefresh(pos){
	if( !_listTable() )return;
	if( !defined(pos) )pos = _listSelRow() ? _listSelPos() : 0;
	var filter = _listFilter();
	var sort = _listSort();
	var table = _listTable();
	oWS.cb = function(ret){__cb(ret,pos,filter,sort)};
	getList();
	///oWS.co.async = false;
	///var ret = WS(oWS.co);
	///if( showSOAPError(ret) )return;
	///__cb(ret,pos,filter,sort);
	// Locals
	function __cb(ret,pos,filter,sort){// call back for list refresh after getList()
		if( showSOAPError(ret) )return;
		loadDSO(dso,ret.value);
		_listFilter(filter);
		_listSort(sort);
		if( _listRecordCount() == 0 )return;
		if( pos >= _listRecordCount() )pos = 0;
		if( pos >= 0 )table.onready = __click;
		//table.attachEvent("onreadystatechange",__click);
		// Locals
		function __click(){//click on row after rs was populated in table list
			if(table.readyState!="complete")return;
			status = "_click.pos:"+pos;
			setTimeout(function(){try{_listTable().DoClick(pos);}catch(e){}},100);
			table.onready = null;
			//table.detachEvent("onreadystatechange",arguments.callee);
		}
		function __findRowByFieldValue(field,value){
			var rs = _listRS();
			rs.MoveFirst();
			rs.Find(field+" = "+value);
			return rs.EOF ? -1 : rs.AbsolutePosition - 1;
		}
	}
}
function attachBehavior(tbl,thread){
	if( tbl.readyState != "complete" )return;
//	if( tbl.behaviorUrns.length || tbl.behaviorUrns[tbl.behaviorUrns.length-1] != "" )return;
	if( !thread ){
		clearTimeout(attachBehavior.proc);
		attachBehavior.proc = setTimeout(function(){attachBehavior(tbl,true)},100);
		return;
	}
	tbl.addBehavior("/include/HTC/rowover_js.htc");
	try{
		var rs = eval(tbl.dataSrc.substr(1)).recordset;
		status = rs.recordCount+" item"+(rs.recordCount==1?"":"s")+".";
	}catch(e){}
}
function features(add){
var s = ",status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes,top=0,left=0,"+(add||'');
var statusBar = s.match(/status=yes/i)?true:false;
	return "height="+(screen.availHeight-30-(statusBar?30:0))+",width="+(screen.availWidth-10)+s;
}
// ***** Properties
function _listFilter(v){
	var rs = _listRS();
	if( defined(v) )rs.Filter = v;
	return rs.Filter;
}
function _listSort(v){
	var rs = _listRS();
	if( defined(v) )rs.Sort = v;
	return rs.Sort;
}
function _listPos(row){return ((row||{}).rowIndex||0)-1}
function _listRS(){return dso.recordset}
function _listRecordCount(){return _listRS().recordCount}
function _listTable(){return contents.all.tblTempl}
function _listBodies(){return _listTable().tBodies}
function _listRow(pos){return _listBodies()[pos].rows[0]}
function _listSelRow(){return (_listTable()||{}).selRow}
function _listSelPos(){return _listPos(_listSelRow())}
function _listMenuID(v){
	if( defined(v) )_listMenuID.menuID = v;
	return _listMenuID.menuID;
}
function _filters(){return _listTable().tHead.all.tags("INPUT")}
function _menuPath(v){
	if( arguments.length )spnMenuPath.innerHTML = v;
	return spnMenuPath.innerText;
}